/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sun.ShipperProcessor;

import com.sun.Entity.Shippingorder;
import com.sun.Entity.Truck;
import com.sun.Facade.ShippingorderFacadeLocal;
import com.sun.Facade.TruckFacadeLocal;
import exceptions.EstimatedShippingDateAlreadyCalculatedException;
import exceptions.InvalidEstimatedEndProductionDateException;
import exceptions.UnknownOrderException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.ejb.Stateless;
import javax.jws.WebParam;

/**
 *
 * @author test
 */
@WebService()
@Stateless()
public class EstimateShippingDateWS {

    @EJB
    private TruckFacadeLocal truckFacade;
    @EJB
    private ShippingorderFacadeLocal shippingorderFacade;

    /**
     * Web service operation
     */
    @WebMethod(operationName = "EstimateShippingDate")
    public void EstimateShippingDate(
            @WebParam(name = "OrderID") String OrderID//,
            //   @WebParam(name = "EstimatedEndProductionDate") Date EstimatedEndProductionDate
            )
            throws
            UnknownOrderException,
            InvalidEstimatedEndProductionDateException,
            EstimatedShippingDateAlreadyCalculatedException {
        // Verification OrderID existance
        Shippingorder Order = shippingorderFacade.find(OrderID);
        if (Order == null) {
            throw new UnknownOrderException();
        }

        // 2013-01-23
        Date EstimatedEndProductionDate = Calendar.getInstance().getTime();
                 Calendar c = Calendar.getInstance();

        c.setTime(EstimatedEndProductionDate);
        c.add(Calendar.DATE, 8);
        EstimatedEndProductionDate = c.getTime();
        // Get availability of truck with sql request (not exist ...)

        // update the order with the truck ID
        while (true) {
            // update the order with the esitmate shipping date calculated
             c = Calendar.getInstance();
            c.setTime(EstimatedEndProductionDate);
            c.add(Calendar.DATE, 2);
            Date EstimatedShippingDate = c.getTime();


            List<Shippingorder> noavail = shippingorderFacade.findAvailability(EstimatedEndProductionDate, EstimatedShippingDate);

            List<Truck> alltruck = truckFacade.findAll();

            for (Shippingorder order : noavail) {
                if (alltruck.contains(order.getTruckid())) {
                    alltruck.remove(order.getTruckid());
                }
            }

            c.setTime(EstimatedEndProductionDate);
            c.add(Calendar.DATE, 1);
            EstimatedEndProductionDate = c.getTime();

            if (alltruck.size() != 0) {
                Order.setTruckid(alltruck.get(0));
                Order.setEstimatedshippingdate(EstimatedEndProductionDate);
                shippingorderFacade.edit(Order);
                break;
            }
        }
    }
}
